package org.jeecg.modules.controller.wx;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.result.Result;
import org.jeecg.common.util.result.ResultMapper;
import org.jeecg.modules.controller.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.weixin4j.WeixinException;
import org.weixin4j.model.js.WxConfig;
import org.weixin4j.spring.WeixinTemplate;

import javax.annotation.Resource;

/**
 * @author guoyh
 * @ClassName WxController
 * @Description
 * @since 2020/8/29 03:19
 **/
@RestController
@RequestMapping("api/v1/wx")
@Slf4j
public class WxController extends BaseController {

    @Resource
    private WeixinTemplate weixinTemplate;

    @GetMapping("sign")
    public Result<WxConfig> jsSdkSign(@RequestParam("url") String url){
        try {
            log.info("微信签名url:{}",url);
            WxConfig wxConfig = weixinTemplate.js().getWxConfig(url);
            log.info("微信签名结果:{}", JSONUtil.toJsonStr(wxConfig));
            return ResultMapper.<WxConfig>ok().setData(wxConfig);
        } catch (WeixinException e) {
            log.error("获取微信签名失败",e);
            return ResultMapper.error();
        }
    }
}
